<?php
class PostController extends FrontendController
{
	public $layout = 'column1';
	public function actionRead()
	{
		$postId = Yii::app()->request->getParam('id');
		$data = FrontendNewsModel::model()->findByPk($postId);
		$data->views = $data->views+1;
		$data->save();
		$newsReleat = FrontendNewsModel::getArticle(5, 0, " AND catid= ".$data->catid." AND id<>".$postId);
		$newsViews = FrontendNewsModel::getArticleByCat(9, 5, 0, '', 'ORDER BY tc.views DESC');
		$this->render('read',array('data'=>$data, 'newsReleat'=>$newsReleat, 'newsViews'=>$newsViews));
	}
	public function actionList()
	{
		$this->render('list');
	}
	public function actionNews()
	{
		$this->render('list');
	}
	public function actionSearch()
	{
		$keyword = Yii::app()->request->getParam('keyword');
		$criteria = new CDbCriteria();
		$criteria->compare('title', $keyword, true);
		$criteria->compare('type', 'post');
		$result =  new CActiveDataProvider('FNews', array(
			'criteria' => $criteria,
			'sort'=>array(
            	'defaultOrder'=>'id DESC',
	        ),
	        'pagination'=>array(
	            'pageSize'=>20
	        ),
		));
		$this->render('search', array('result'=>$result, 'keyword'=>$keyword));
	}
	public function actionCategory()
	{
		$catId = Yii::app()->request->getParam('id', 0);
		$catName = FrontendCategoriesModel::model()->findByPk($catId);
		$posts = FrontendNewsModel::getArticleByCat($catId);
		$count = FrontendNewsModel::getTotalArticleByCat($catId);
		
		$page = new CPagination($count);
		$itemOnPaging = 5;
		$page->pageSize = Yii::app()->params['news_perpage'];
		$curr_page = $page->getCurrentPage();
		$data = FrontendNewsModel::getArticleByCat(9, $page->getLimit(), $page->getOffset());
		
		$newsViews = FrontendNewsModel::getArticleByCat($catId, 5, 0, '', 'ORDER BY tc.views DESC');
		$this->render('category', array(
				'data'=>$posts,
				'page'=>$page,
				'itemOnPaging'=>$itemOnPaging,
				'catName'=>$catName,
				'catId'=>$catId,
				'count'=>$count,
				'newsViews'=>$newsViews
		));
	}
}